//
//  HNSH_TZExchangeRecordViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/14.
//

#import "HNSH_TZExchangeRecordViewController.h"

#import "HNSH_TZExchangeRecordCell.h"
#import "HNSH_TZExchangeRecordHeader.h"

@interface HNSH_TZExchangeRecordViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;
/* headerAry */
@property (nonatomic, strong) NSMutableArray *headerAry;

/* currentMonth */
@property (nonatomic, assign) NSInteger currentMonth;

@end

static NSString *HNSH_TZExchangeRecordCellID = @"HNSH_TZExchangeRecordCellID";
static NSString *HNSH_TZExchangeRecordHeaderID = @"HNSH_TZExchangeRecordHeaderID";

@implementation HNSH_TZExchangeRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"兑换记录" showLeftItem:YES];
    
    [self configTableView];
}

- (void)configTableView {
    [self.tableView registerClass:[HNSH_TZExchangeRecordCell class] forCellReuseIdentifier:HNSH_TZExchangeRecordCellID];
    [self.tableView registerClass:[HNSH_TZExchangeRecordHeader class] forHeaderFooterViewReuseIdentifier:HNSH_TZExchangeRecordHeaderID];
    
    self.tableView.backgroundColor = [UIColor appBGColor];
    self.tableView.rowHeight = 62;
    self.tableView.estimatedRowHeight = 62;
    
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
}


- (void)refreshData:(NSInteger)page {
    NSDictionary * params = @{@"account_type":@"11",
                              @"business_types":@"11010,11011",
                              @"income_type":@"",
                              @"page_size":@"10",
                              @"page_num":[NSString stringWithFormat:@"%ld", page]};
    [HNSH_UserNetServer requestBillListWithParams:params success:^(ST_BaseNetModel * _Nullable responseObject) {
        [self endRefresh:YES];
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
                [self.headerAry removeAllObjects];
                self.currentMonth = 0;
            }
            NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_TZExchangeRecordModel class] json:responseObject.resultset[@"list"]];
            [self configListDataWithAry:ary];

//            NSArray *headerAry = [NSArray yy_modelArrayWithClass:[HNSH_TZExchangeRecordHeaderModel class] json:responseObject.resultset[@"statistic_list"]];
//            if (headerAry.count > 0 && self.headerAry.count > 0) {
//                HNSH_TZExchangeRecordHeaderModel *model_last = self.headerAry.lastObject;
//                HNSH_TZExchangeRecordHeaderModel *model_first = headerAry.firstObject;
//                if ([model_last.date_time isEqualToString:model_first.date_time]) {
//                    [self.headerAry removeLastObject];
//                }
//            }
//            [self.headerAry addObjectsFromArray:headerAry];

            [self endRefresh:ary.count>=10];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

- (NSMutableArray *)headerAry {
    if (!_headerAry) {
        _headerAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _headerAry;
}

- (void)configListDataWithAry:(NSArray *)ary {
    if (ary.count <= 0) {
        return;
    }
    NSMutableArray *items;
    for (HNSH_TZExchangeRecordModel *model in ary) {
        if (self.currentMonth == [self getMonthWithString:model.create_time]) {
            if (!items) {
                items = self.listAry.lastObject;
            }
            [items addObject:model];
        } else {
            self.currentMonth = [self getMonthWithString:model.create_time];
            [self.headerAry addObject:[NSString stringWithFormat:@"%ld",self.currentMonth]];
            items = [NSMutableArray arrayWithCapacity:0];
            [self.listAry addObject:items];
            [items addObject:model];
        }
    }
}

- (NSInteger)getMonthWithString:(NSString *)time {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    //设置时区选择北京时间
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:time];
    //下面是单独获取每项的值
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitWeekday |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:date];
    return [comps month];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *ary = self.listAry[section];
    return ary.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 94.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HNSH_TZExchangeRecordHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_TZExchangeRecordHeaderID];
    header.monthString = self.headerAry[section];
    return header;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_TZExchangeRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_TZExchangeRecordCellID];
    cell.model = self.listAry[indexPath.section][indexPath.row];
    return cell;
}


@end
